package com.zzh.tools.utils;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.TextUtils;


import com.zzh.tools.system.ToolThrow;

import java.io.Serializable;

/**
 * @author zhangzhihao
 * @email zhangzhihao@gongjiangren.net
 * @date 2020-07-24
 * @desc
 */
public class ToolIntent {


    /**
     * 创建带有参数的intent函数
     *
     * @param objects key 和 value 顺序填写
     * @return
     */
    public static Intent createIntent(Object... objects) {
        ToolThrow.isLengthEvenNumberThrow(objects);
        Intent intent = new Intent();
        for (int i = 0; i < objects.length; i += 2) {
            String key = (String) objects[i];
            Object value = objects[i + 1];
            if (value instanceof Integer) {
                intent.putExtra(key, (int) value);
            } else if (value instanceof String) {
                intent.putExtra(key, (String) value);
            } else if (value instanceof Parcelable) {
                intent.putExtra(key, (Parcelable) value);
            } else if (value instanceof Double) {
                intent.putExtra(key, (Double) value);
            } else if (value instanceof Float) {
                intent.putExtra(key, (Float) value);
            } else if (value instanceof Byte) {
                intent.putExtra(key, (Byte) value);
            } else if (value instanceof Short) {
                intent.putExtra(key, (Short) value);
            } else if (value instanceof Long) {
                intent.putExtra(key, (Long) value);
            } else if (value instanceof Serializable) {
                intent.putExtra(key, (Serializable) value);
            }
        }
        return intent;
    }


    /**
     * 比对一组参数和上层参数有没有变化
     *
     * @param intent
     * @param o
     * @return
     */
    public static boolean compareParameters(Intent intent, Object... o) {
        ToolThrow.isLengthEvenNumberThrow(o);
        ToolThrow.isNullObjectThrow(intent);
        Bundle bundle = intent.getExtras();
        if (bundle == null) {
            ToolLog.i("zzh", "没有什么可以比对的数据");
            return false;
        }
        return ToolBundle.compareParameters(bundle, o);
    }


    /**
     * 比对一个参数和上层参数有没有变化
     *
     * @param intent 传入的intent
     * @param key    key值
     * @param value  值
     * @return
     */
    public static boolean compareParameter(Intent intent, String key, Object value) {
        ToolThrow.isNullObjectThrow(intent);
        Bundle bundle = intent.getExtras();
        if (bundle == null) {
            ToolLog.i("zzh", "没有什么可以比对的数据");
            return false;
        }
        return ToolBundle.compareParameter(bundle, key, value);
    }
}
